﻿using DeviceAlarmService.Models.Message;
using DeviceAlarmService.SystemService;

namespace DeviceAlarmService.MessagePush.Communication
{
    public static class PushService
    {
        /// <summary>
        /// 操作委托
        /// </summary>
        /// <param name="status"></param>
        public delegate void LogDelegate(string log);
        /// <summary>
        /// 操作事件
        /// </summary>
        /// <param name="status"></param>
        public static event LogDelegate LogEvent;

        public static void OperationLog(string log)
        {
            if (LogEvent is not null)
            {
                LogEvent($"{log}");
            }
        }

        private static Queue<Models.Information> TaskQueue { get; set; }

        /// <summary>
        /// 开启拨号服务
        /// </summary>
        public static void StartService()
        {
            new Task(() =>
            {
                //确认服务通讯类型
                ICommunication communication = new NullCommunication();

                if (ConfigService.Configuration.passageway == MessageEnum.Socket)
                {
                    communication = new SocketCommunication();
                }
                if (ConfigService.Configuration.passageway == MessageEnum.SerialPort)
                {
                    communication = new SerialPortCommunication();
                }
                if (communication.Connect())
                {
                    OperationLog($"远程推送服务连接建立");
                }
                else
                {
                    OperationLog($"远程推送服务连接失败");
                }
                while (true)
                {
                    if (TaskQueue is not null && TaskQueue.Count >= 1)
                    {
                        Models.Information information = TaskQueue.Dequeue();
                        //拨号
                        foreach (string item in information.contacts)
                        {
                            if (communication.IsConnected)
                            {
                                if (information.messageType == MessageTypeEnum.SMS)
                                {
                                    communication.SendSMS(item, information.message, out _);
                                }
                                if (information.messageType == MessageTypeEnum.CALL)
                                {
                                    communication.SendCall(item, information.message, out _);
                                }
                            }
                            else
                            {
                                //重新连接服务
                                _ = communication.Connect();
                                OperationLog($"重新连接远程推送服务");
                            }
                            //间隔1秒
                            Thread.Sleep(1000);
                        }
                    }
                    Thread.Sleep(1000);
                }
            }).Start();
        }

        /// <summary>
        /// 添加服务
        /// </summary>
        /// <param name="messageType">方式</param>
        /// <param name="contacts">联系人</param>
        /// <param name="message">信息</param>
        public static void Add(MessageTypeEnum messageType, List<string> contacts, string message)
        {
            //检查接收人是否存在
            if (contacts.Count <= 0)
            {
                return;
            }
            if (ConfigService.Configuration.enablePushService == false)
            {
                return;
            }

            Models.Information information = new();
            information.contacts = contacts;
            information.message = message;
            information.messageType = messageType;

            if (TaskQueue is not null)
            {
                TaskQueue.Enqueue(information);
            }
            else
            {
                TaskQueue = new();
                TaskQueue.Enqueue(information);
            }
        }
    }
}
